package com.jp.knowledge.logic;

/**
 * Created by jiayen on 17/6/1.
 */

import android.content.Context;

import com.google.gson.JsonObject;
import com.jiayen.dialog.LoadingDialog;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.comm.JpApplication;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.WeChatModel;
import com.jp.knowledge.util.ResultSubscriber;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.xutils.common.util.MD5;

/***
 * 支付logic
 */
public class PayLogic implements ResultSubscriber.OnResultListener {
    private Context mContext;

    private IWXAPI api;

    private LoadingDialog  loadingDialog;

    public PayLogic(Context context){
        this.mContext=context;
        loadingDialog = new LoadingDialog(mContext);
        initIWXAPI();
    }
    private void initIWXAPI(){
        api = WXAPIFactory.createWXAPI(mContext, JpApplication.weixin_appid, true);
        api.registerApp(JpApplication.weixin_appid);
    }

    /***
     * 开始支付
     * @param price
     * @param toId
     * @param toUserId
     * @param toType
     */
    public void pay(int price,String toId,String toUserId,int toType,String body,int payType,String packageid){
        JsonObject param = new JsonObject();
        param.addProperty("total_fee", price);    // 金额
        param.addProperty("toId", toId);                              // 团队圈 Id
        param.addProperty("toUserId", toUserId);                              // 打赏用户 Id
        param.addProperty("body",body);                            // 描述
        param.addProperty("trade_type", "APP");                             // 交易类型
//        param.addProperty("spbill_create_ip", NetUtil.getIPAddress(this)); // 客户端IP
        param.addProperty("toType", toType);                                // 支付对象类型（接口文档）
        param.addProperty("payType", payType);                                     // 支付类型( 1提现 2赞赏 3会员充值 4商城支付)
        param.addProperty("payMethod", "WECHATPAY");
        // 支付方法（WECHATPAY-微信支付  JPPAY-竞品说钱包支付）
        JsonObject object = new JsonObject();
        object.addProperty("packageid",packageid);
        object.addProperty("identity",HttpHelper.getInstance(mContext).getDevieceToken());
        param.addProperty("attach",object.toString());
        HttpHelper.getInstance(mContext).wechatPay(param, 0x01, this);
    }
    @Override
    public void onStart(int requestType) {
        loadingDialog.show();
    }

    @Override
    public void onCompleted(int requestType) {
        loadingDialog.cancel();
    }

    @Override
    public void onError(int requestType) {
        onCompleted(requestType);
    }

    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            onError(requestType);
            ToasUtil.toast(mContext, data.getMessage());
            return;
        }
        WeChatModel weChatModel = data.getEntity(WeChatModel.class);
        PayReq req = new PayReq();
        req.appId = weChatModel.getAppid();
        req.nonceStr = weChatModel.getNonce_str();
        req.packageValue = "Sign=WXPay";
        req.partnerId = weChatModel.getMch_id();
        req.prepayId = weChatModel.getPrepay_id();
        req.timeStamp = System.currentTimeMillis()/1000+"";
        String sign = "appid=" + req.appId
                + "&noncestr=" + req.nonceStr
                + "&package=" + req.packageValue
                + "&partnerid=" + req.partnerId
                + "&prepayid=" + req.prepayId
                + "&timestamp=" + req.timeStamp
                + "&key=" +JpApplication.weixin_key;
        req.sign = MD5.md5(sign).toUpperCase();
        api.sendReq(req);
    }
}
